<?php

namespace addons\member\model;

class MemberAchievement extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'member_achievement';
    }


    public function createPhy($user_id){
    	$info['user_id'] = $user_id;
        $info['update_time'] = NOW_DATETIME;
        $info['create_time'] = NOW_DATETIME;
        return $this->add($info);
    }


    /**
     * 添加团队人数
     * @param type 1-push_total 2-push_auth_total
     */
    public function addPhy($pid_path,$amount,$type=1){
    	if($type==1){
    		$field = 'push_total';
    	}elseif($type==2){
    		$field = 'push_auth_total';
    	}else{
            return 1;
        }
        $arr = array_filter(array_unique(explode(',', $pid_path)));
        if($amount>0){
            return $this->where(['user_id'=>['in',$arr]])->setInc($field,$amount);
        }else{
            $amount = abs($amount);
            return $this->where(['user_id'=>['in',$arr]])->setDec($field,$amount);
        }
    }

    /**
     * 团队业绩计算
     */
    public function teamStrengthCal($user_id,$strength,$type = true){
        if($strength>0){
            $res = $this->where('user_id',$user_id)->value('id');
            if(!$res) $this->createPhy($user_id);
            $pid_path = (new \addons\member\model\MemberAccountModel)->where('id',$user_id)->value('pid_path');
            $arr = array_filter(explode(',', $pid_path)) ;
            if($type){
                $res = $this->where(['user_id'=>['in',$arr]])->setInc('team_strength',$strength);
            }else{
                $res = $this->where(['user_id'=>['in',$arr]])->setDec('team_strength',$strength);
            }
        }else{
            $res = 1;
        }
        // $list = $m->field('user_id,team_strength')->where(['user_id'=>['in',$pid_path]])->select();
        // foreach ($list as $k => $v) {
        //     $this->talentCalcul($v['user_id'],$v['team_strength']);
        // }
        return $res;
    }

    /**
     * 团队业绩计算
     */
    public function shopAchCal($user_id,$strength,$type = true){
        if($strength>0){
            $res = $this->where('user_id',$user_id)->value('id');
            if(!$res) $this->createPhy($user_id);
            $pid_path = (new \addons\member\model\MemberAccountModel)->where('id',$user_id)->value('pid_path');
            // $arr = array_filter(explode(',', $pid_path)) ;
            // $arr[] = $user_id;
            if($type){
                $res = $this->where(['user_id'=>['in',$pid_path]])->setInc('shop_ach',$strength);
            }else{
                $res = $this->where(['user_id'=>['in',$pid_path]])->setDec('shop_ach',$strength);
            }
        }else{
            $res = 1;
        }
        // $list = $m->field('user_id,team_strength')->where(['user_id'=>['in',$pid_path]])->select();
        // foreach ($list as $k => $v) {
        //     $this->talentCalcul($v['user_id'],$v['team_strength']);
        // }
        return $res;
    }

    /**
     * 获取推荐人数
     */
    public function getAchTotal($user_id,$userM){
        $count = $userM->alias('m')->join('equip_order o','o.user_id=m.id')->where('pid',$user_id)->sum('o_amount');
        $sum = $this->alias('c')->join('member_pid m','c.user_id=m.id')->where('pid',$user_id)->sum('team_strength');
        return $count+$sum;
    }

}
